package com.tuzhi.sys.service.impl;

import com.tuzhi.base.jpa.healper.QueryHelper;
import com.tuzhi.base.jpa.repository.BaseRepo;
import com.tuzhi.base.service.impl.AbsBaseService;
import com.tuzhi.sys.jpa.entity.SysUserRoleEntity;
import com.tuzhi.sys.jpa.entity.pk.SysUserRolePk;
import com.tuzhi.sys.jpa.repository.SysUserRoleRepo;
import com.tuzhi.sys.service.SysUserRoleService;
import com.tuzhi.util.CollectionsTzUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
public class SysUserRoleServiceImpl extends AbsBaseService<SysUserRoleEntity, SysUserRolePk> implements SysUserRoleService {

    private static final Logger log = LoggerFactory.getLogger(SysUserRoleServiceImpl.class);

    @Autowired
    SysUserRoleRepo repo;

    @Override
    protected BaseRepo getThisRepo() {
        return repo;
    }

    @Override
    public List<Long> listRoleId4UserId(Long userId) {
        List<Long> listId = new ArrayList<>();
        QueryHelper.Builder<SysUserRoleEntity> builder = new QueryHelper.Builder<>();
        builder.eq(SysUserRoleEntity::getUserId, userId);
        List<SysUserRoleEntity> sysRoleMenuEntities = repo.findAll(builder.build().getWhereConditionBO());
        for (SysUserRoleEntity entity : sysRoleMenuEntities) {
            listId.add(entity.getRoleId());
        }
        return listId;
    }

    @Override
    public void saveUserRole(Long userId, Collection<Long> roleIds) {
        List<Long> dbIds = listRoleId4UserId(userId);
        //找出要新增的项
        Collection<Long> addIds = CollectionsTzUtil.getAddItem(dbIds, roleIds);
        addIds.forEach(item -> {
            SysUserRoleEntity sysUserRoleEntity = new SysUserRoleEntity();
            sysUserRoleEntity.setRoleId(item);
            sysUserRoleEntity.setUserId(userId);
            repo.save(sysUserRoleEntity);
            log.info("用户：{}，新增加角色：{}", userId, item);
        });

        //找出删除的项
        Collection<Long> delIds = CollectionsTzUtil.getDeleteItem(dbIds, roleIds);
        delIds.forEach(delItem -> {
            SysUserRolePk pk = new SysUserRolePk();
            pk.setRoleId(delItem);
            pk.setUserId(userId);
            repo.deleteById(pk);
            log.info("用户：{}，从角色：{}中移除", userId, delItem);
        });
    }

    @Override
    public int countByRoleId(Long roleId) {
        return repo.countByRoleId(roleId);
    }

    @Transactional
    @Override
    public int deleteByRoleId(Long roleId) {
        int count = countByRoleId(roleId);
        if (count > 0) {
            return repo.deleteByRoleId(roleId);
        }
        return 0;
    }

    @Override
    public int countByUserId(Long userId) {
        return repo.countByUserId(userId);
    }

    @Override
    public int deleteByUserId(Long userId) {
        int count = countByUserId(userId);
        if (count > 0) {
            return repo.deleteByUserId(userId);
        }
        return 0;
    }

//    查询角色所有的用户id
    @Override
    public List<SysUserRoleEntity> listUserIdByRoleId(Long id) {
        QueryHelper.Builder<SysUserRoleEntity> builder = new QueryHelper.Builder<>();
        builder.eq(SysUserRoleEntity::getRoleId, id);
        List<SysUserRoleEntity> sysRoleMenuEntities = repo.findAll(builder.build().getWhereConditionBO());
        return sysRoleMenuEntities;
    }


}
